import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:hkzf/utils/common_toast.dart';
import 'package:hkzf/utils/dio_http.dart';
import 'package:hkzf/utils/string_is_null_or_empty.dart';

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  var usernameController = TextEditingController();
  var passwordController = TextEditingController();
  var confirmPasswordController = TextEditingController();
  _registerHandler() async {
    var username = usernameController.text;
    var password = passwordController.text;
    var confirmPassword = confirmPasswordController.text;
    if (stringIsNullOrEmpty(username) || stringIsNullOrEmpty(password)) {
      CommonToast.showToast('账号或密码不能为空');
      return;
    }
    if (confirmPassword != password) {
      CommonToast.showToast('两次输入的密码不一致');
      return;
    }
    var res = await DioHttp.of(context).post('/api/v1/auth/register',
        params: {'username': username, 'password': password});
    if (res == null) {
      return;
    }
    if (res['code'] == 200) {
      CommonToast.showToast('注册成功');
      Navigator.of(context).pushReplacementNamed('login');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('注册'),
        ),
        body: SafeArea(
          minimum: EdgeInsets.all(30),
          child: ListView(
            children: <Widget>[
              Column(
                children: [
                  TextField(
                    controller: usernameController,
                    decoration:
                        InputDecoration(hintText: '请输入用户名', labelText: '用户名'),
                  ),
                  Padding(
                    padding: EdgeInsets.all(10),
                  ),
                  TextField(
                    controller: passwordController,
                    decoration: InputDecoration(
                      hintText: '请输入密码',
                      labelText: '密码',
                    ),
                    obscureText: true,
                  ),
                  Padding(
                    padding: EdgeInsets.all(10),
                  ),
                  TextField(
                    controller: confirmPasswordController,
                    decoration: InputDecoration(
                      hintText: '请输入密码',
                      labelText: '确认密码',
                    ),
                    obscureText: true,
                  ),
                  Padding(
                    padding: EdgeInsets.all(10),
                  ),
                  Container(
                    width: double.infinity,
                    child: RaisedButton(
                      onPressed: _registerHandler,
                      color: Colors.green,
                      child: Text(
                        '注册',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                  Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                    Text('已有账号,'),
                    FlatButton(
                      onPressed: () {
                        Navigator.pushReplacementNamed(context, '/register');
                      },
                      child: Text(
                        '去登录~',
                        style: TextStyle(color: Colors.green),
                      ),
                    )
                  ])
                ],
              ),
            ],
          ),
        ));
  }
}
